<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/18
 * Time: 9:37
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\model\mysql;

use app\common\model\TimeModel;

class MallGoods extends TimeModel
{
    protected $deleteTime = 'delete_time';

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTopTenByList(): array
    {
        return $this
            ->field("id,title,logo,price")
            ->where("(!find_in_set('10',cate_ids) or !find_in_set('11',cate_ids) or !find_in_set('12',cate_ids) or !find_in_set('13',cate_ids) or !find_in_set('14',cate_ids)) and status=1")
            ->order('sales desc,sort desc,id desc')
            ->limit(10)
            ->select()
            ->toArray();
    }

    /**
     * 查询指定商品
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIsAppointByList(string $field='*'){
        return $this->field($field)
            ->where('is_appoint',1)
            ->where('status',1)
            ->order('sort desc,id desc')
            ->select()
            ->toArray();
    }

    /**
     * 查询指定商品详情
     * @param int $goods_id
     * @param string $field
     * @return mixed
     */
    public function  getGoodsDetail(int $goods_id=0,string $field='*'){
        return $this->field($field)->where("id",$goods_id)->find();
    }

    /**
     *
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetSpecsValueIdsAttr($value){
        if(empty($value)){
            return '';
        }
        $ids = explode("-",$value);
        $res= (new MallSpecsValue())->where('id','in',$ids)
        ->select()->toArray();
        return array_column($res,'title');
    }
}